package com.ruoyi.web.controller.project;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.ObjectUtil;
import com.dgy.project.domain.ProjectNotice;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.dgy.project.service.IProjectNoticeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 课题申报通知Controller
 *
 * @author ccc
 * @date 2025-03-13
 */
@RestController
@RequestMapping("/project/notice")
public class ProjectNoticeController extends BaseController
{
    @Autowired
    private IProjectNoticeService ProjectNoticeService;

    /**
     * 查询课题申报通知列表
     */
    @PreAuthorize("@ss.hasPermi('project:notice:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProjectNotice ProjectNotice)
    {
        startPage();
        List<ProjectNotice> list = ProjectNoticeService.selectProjectNoticeList(ProjectNotice);
        return getDataTable(list);
    }

    /**
     * 导出课题申报通知列表
     */
    @PreAuthorize("@ss.hasPermi('project:notice:export')")
    @Log(title = "课题申报通知", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProjectNotice ProjectNotice)
    {
        List<ProjectNotice> list = ProjectNoticeService.selectProjectNoticeList(ProjectNotice);
        ExcelUtil<ProjectNotice> util = new ExcelUtil<ProjectNotice>(ProjectNotice.class);
        util.exportExcel(response, list, "课题申报通知数据");
    }

    /**
     * 获取课题申报通知详细信息
     */
    @PreAuthorize("@ss.hasPermi('project:notice:query')")
    @GetMapping(value = "/{noticeId}")
    public AjaxResult getInfo(@PathVariable("noticeId") Long noticeId)
    {
        // 获取通知详情
        ProjectNotice projectNotice = ProjectNoticeService.selectProjectNoticeByNoticeId(noticeId);
        if(ObjectUtil.isEmpty(projectNotice)){
            return error("该申报通知不存在");
        }
        // 更新点击次数
        projectNotice.setClickNum(projectNotice.getClickNum()+1);
        ProjectNoticeService.updateProjectNotice(projectNotice);
        return success(projectNotice);
    }

    /**
     * 新增课题申报通知
     */
    @PreAuthorize("@ss.hasPermi('project:notice:add')")
    @Log(title = "课题申报通知", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProjectNotice projectNotice)
    {
        // 获取当前登陆用户信息
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        // 补充添加人
        projectNotice.setCreateBy(user.getNickName());
        return toAjax(ProjectNoticeService.insertProjectNotice(projectNotice));
    }

    /**
     * 修改课题申报通知
     */
    @PreAuthorize("@ss.hasPermi('project:notice:edit')")
    @Log(title = "课题申报通知", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProjectNotice projectNotice)
    {
        // 获取当前登陆用户信息
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        // 补充更新人
        projectNotice.setCreateBy(user.getNickName());
        return toAjax(ProjectNoticeService.updateProjectNotice(projectNotice));
    }

    /**
     * 删除课题申报通知
     */
    @PreAuthorize("@ss.hasPermi('project:notice:remove')")
    @Log(title = "课题申报通知", businessType = BusinessType.DELETE)
    @DeleteMapping("/{noticeIds}")
    public AjaxResult remove(@PathVariable Long[] noticeIds)
    {
        return toAjax(ProjectNoticeService.deleteProjectNoticeByNoticeIds(noticeIds));
    }
}
